<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<title>武汉理工大学出版社</title>
		<script src="js/jquery-3.2.1.min.js"></script>
		<script src="js/rem.js"></script>
		<link rel="stylesheet" href="css/reset.css">
		<link rel="stylesheet" href="css/page_div.css">
		<script src="js/v3.2.8/vue.global.prod.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/axios.min.js"></script>
		<link rel="stylesheet" href="css/style.css">
	</head>	
	<body style="background-color: #fff;">
		<!-- 头部 -->
		<div id="header"></div>
		
		<div id="bootstrap"></div>
		
		<!-- 新书推荐 -->
		<div class="news" id="app">
			<div class="recommend">
				<ul>
					<li v-for="(item,index) in booksList"  @click="openDetail(item.id)">
						<img :src="imgUrl+item.coverImg">
						<div class="content">
							<p class="title">{{item.bookName}}</p>
							<p class="text">作者：{{item.bookAuthor}}</p>
							<p class="text">出版时间：{{item.publishTime}}</p>
							<p class="text">ISBN：{{item.isbn}}</p>
							<p class="text" style="margin: 0;">定价：<span>￥{{item.discountPrice}}</span></p>
						</div>
					</li>
				</ul>
			</div>
			<div v-cloak class="app">
				<div v-show="total >= 1" class="pageContainer">
					<ul class="pagesInner">
<!--						<li class="page" @click="prevOrNext(-1)"><span aria-hidden="true">&lt;</span></li>-->
						<li class="page" v-for="(item, index) in pages" :key="index" :class="{actived: item == pageIndex}" @click="selectPage(item)">
							<span>{{item}}</span>
						</li>
<!--						<li class="page" @click="prevOrNext(1)"><span class="fa fa-chevron-right" aria-hidden="true">&gt;</span></li>-->
					</ul>
					共{{total}}页&nbsp;转到第&nbsp;
					<input type="text" v-model="goToPage" class="totalinput">
					&nbsp;页&nbsp;
					<button @click="handleGoToPage">GO</button>
				</div>
			</div>
			
			<!-- 加载中 -->
			<div class="load_page" v-show="isLoadingShow">
				<div class="content">
					<div class="item">
						<i class="loader --1"></i>
					</div>
				</div>
			</div>
		</div>
		<!-- 底部 -->
		<div id="footer"></div>
	</body>
	
	<script>
		const App = {
			data() {
				return {
					imgUrl:'http://dbs.wutp.com.cn/CM-API',
					booksList:[],
					pageIndex: 1,
					pageSize: 15,
					goToPage: '',
					total:1,
					isLoadingShow:false
				}
			},
			computed: {
				pages: function () {
					// 每次最多显示5个页码
					var c = this.pageIndex
					var t = this.total
					var p = []
					for (var i = 1; i <= t; i++) {
						p.push(i)
					}
					var l = p.length
					if (l <= 5) { // 总页数<=5，显示全部页码
						return p
					} else if (l > 5 && c <= 4) { // 总页数>5 && 当前页面<=3
						return [1, 2, 3, 4, 5,'...', '尾页']
					} else if (l > 5&& c > 4 && c <= l - 3) { // 总页数 > 5 && 当前页面 > 3 && 当前页 < 总页数 - 3
						return [c - 3, c - 2, c - 1, c, c + 1,  '...', '尾页']
					} else { // 总页数 > 5 && 当前页面 > 3 && 当前页 > 总页数 - 3
						debugger
						return [ '首页',t - 3, t - 2, t - 1,t ]
					}
				},
			},
			mounted(){
				sessionStorage.setItem('href',window.location.href);
				this.getBooksList()
			},
			methods:{
				// 新书推荐
				async getBooksList(){
					let that = this;
					const orderParam={publish_time: false}
					that.isLoadingShow = true
					await axios({
						method:"get",
						url:"http://dbs.wutp.com.cn/CM-API/booksApi/getBooksList?thirdLabel=XSTJ&pageNum="+that.pageIndex+"&pageSize="+that.pageSize,
						params: {orderParam:orderParam},
					}).then(function (res) {
						that.booksList = res.data.data.content;
						// 总条数除以10并向上取整得到总页数
						const total = res.data.data.total / that.pageSize;
						that.total = Math.ceil(total);
						// that.total = res.data.data.total;
						that.pageIndex = res.data.data.pageNum
						that.pageSize = res.data.data.pageSize;
						that.isLoadingShow = false
					})
				},
				prevOrNext: function(n) {
					this.pageIndex += n
					this.pageIndex <= 1 ? this.pageIndex = 1 : this.pageIndex > this.total ? this.pageIndex = this.total : null
					this.getBooksList();
				},
				selectPage: function(n) {
					if(n=='尾页'){
						n = this.total
					}
					if(n=='首页'){
						n = 1
					}
					if (n === this.pageIndex) return
					if (typeof n === 'string') return
					this.pageIndex = n;
					this.getBooksList();
				},
				handleGoToPage: function() {
					this.pageIndex = this.goToPage <= 1 ? 1 : this.goToPage - 0 >= this.total - 0 ? this.total :
							this.goToPage
					this.goToPage = this.pageIndex;
					this.getBooksList();
				},
				openDetail(id){
					let hrefs = window.location.href;
					window.location.href="reDetails.html?name=XSTJ&id="+id+'&href='+hrefs;
				},
			}
		};
		Vue.createApp(App).mount('#app');
	</script>
	
	<style>
		/* banner -------------------------------------------------------------- */
		.banner {
			width: 100%;
			height: 5.71rem;
			position: relative;
		}

		.banner img {
			width: 100%;
		}

		.banner ul {
			width: 3.86rem;
			height: 5.34rem;
			background-color: #D2D0DD;
			position: absolute;
			top: 0;
			left: 1.54rem;
			padding-top: 0.3rem;
		}

		.banner ul li {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 0.7rem;
			padding: 0 0.45rem;
			cursor: pointer;
		}
		
		.banner ul li p{
			font-size: 0.19rem;
		}

		.banner ul li img {
			width: 0.08rem;
		}
		
		/* 新闻动态 ------------------------------------------------------------------ */
		.news{
			margin: 0 3rem;
		}
	
		/* 新书推荐 --------------------------------------------------- */
		.recommend ul{
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			justify-content: space-between;
		}
		.recommend ul li{
			width: 19%;
			border: solid 0.01rem #dddddd;
			padding: 0.14rem 0;
			text-align: center;
			margin-bottom: 0.16rem;
		}
		.recommend ul li img{
			width: 70%;
		}
		.recommend ul li p{
			margin: auto;
		}
		.recommend ul li .title{
			font-size: 0.16rem;
			font-weight: bold;
			margin-top: 0.06rem;
		}
		.recommend ul li .author{
			font-size: 0.14rem;
			color: #666;
			margin-top: 0.06rem;
		}
	</style>
</html>
